package com.metaera.app.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lxj.xpopup.XPopup;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.ActivityDetailsPresenter;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.core.BaseRequestActivity;
import com.metaera.app.ui.popup.SharePopup;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.ShareClickListener;
import com.metaera.app.utils.ViewUtils;
import com.metaera.app.utils.image.GlideApp;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;

public class ActivityDetailsActivity extends BaseRequestActivity {

    public static Intent newInstance(Context context, int id) {
        Intent intent = new Intent(context, ActivityDetailsActivity.class);
        intent.putExtra("ID", id);
        return intent;
    }

    @Override
    protected int getTitleLayoutRes() {
        return R.layout.include_activity_title;
    }

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_details_activity;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ActivityTitleHelper.setTitle(this, R.string.activity_details);
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.layout_title_container));

        // find
        TextView tvDate = findViewById(R.id.tv_date);
        WebView webView = findViewById(R.id.web_view);
        TextView tvTitle = findViewById(R.id.tv_activity_title);
        ImageView ivCover = findViewById(R.id.iv_cover);
        TextView tvOrganizer = findViewById(R.id.tv_organizer);
        TextView tvTickPrice = findViewById(R.id.tv_tick_price);
        TextView tvLocation = findViewById(R.id.tv_location);
        TextView tvGroupNumber = findViewById(R.id.tv_group_number);

        // init
        webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.getSettings().setSupportZoom(false);

        ActivityDetailsPresenter presenter = new ActivityDetailsPresenter();
        presenter.setModelAndView(data -> {
            GlideApp.with(this).load(data.getPic()).normal().into(ivCover);
            tvDate.setText(data.getStart_time() + "~" + data.getEnd_time());
            tvTitle.setText(data.getTitle());
            tvOrganizer.setText(data.getOrganizer());
            tvGroupNumber.setText(String.valueOf(data.getNum()));
            tvLocation.setText(data.getCity() + " " + data.getArea() + " " + data.getAddress());

            tvTickPrice.setText(data.getIs_free() == 1 ? getString(R.string.activity_free) : data.getPrice());

            findViewById(R.id.iv_share_wechat).setOnClickListener(new ShareClickListener(
                    SendMessageToWX.Req.WXSceneSession,
                    data.getTitle(),
                    "",
                    ApiParam.LINK_SHARE_7 + data.getId(),
                    R.mipmap.ic_launcher
            ));

            findViewById(R.id.iv_share_moments).setOnClickListener(new ShareClickListener(
                    SendMessageToWX.Req.WXSceneTimeline,
                    data.getTitle(),
                    "",
                    ApiParam.LINK_SHARE_7 + data.getId(),
                    R.mipmap.ic_launcher
            ));

            // html
            webView.loadData(getHtmlData(data.getContent()), "text/html", "UTF-8");
            webView.loadDataWithBaseURL(null, getHtmlData(data.getContent()), "text/html", "UTF-8", null);
        });

        presenter.execute(getIntent().getIntExtra("ID", 0));
    }


    private String getHtmlData(String bodyHTML) {
        String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:100%; height:auto;}*{margin:0px;}</style>"
                + "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }
}
